package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.Comment;
import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.bean.vo.CommentCountData;
import com.dropdb.demo.bean.vo.CommentListData;
import com.dropdb.demo.service.CommentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wx/comment")
@Validated
public class WxCommentController {
    @Autowired
    CommentService commentService;

    @GetMapping("count")
    public BaseRespVO count(Integer valueId, Byte type) {
        CommentCountData commentCountData = commentService.commentCount(valueId, type);
        return BaseRespVO.ok(commentCountData);
    }

    @GetMapping("list")
    public BaseRespVO list(Integer valueId, Byte type, Integer size, Integer page, Integer showType) {
        CommentListData commentListData = commentService.commentList(valueId, type, size, page, showType);
        return BaseRespVO.ok(commentListData);
    }

    @PostMapping("post")
    public BaseRespVO submitTopicCommit(@RequestBody Comment comment) {
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        Integer insert = commentService.submitTypeCommit(comment, user.getId());
        if (insert != 1) return BaseRespVO.error(500, "专题评论失败");
        return BaseRespVO.ok();
    }
}
